﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com/EmlenMud
using System;
using System.ComponentModel;
using BrainTechLLC;
using System.Drawing;
using System.IO;
using System.Threading;
using BrainTechLLC.WPF;
using System.Diagnostics;
using System.Drawing.Imaging;
using BrainTechLLC.BaseLibrary;
using System.Windows.Media;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;

namespace SimpleFileDataAndViewModels
{
    public static class Helpers
    {
        public static ImageSource LoadImageSource(this Bitmap bitmap, byte[] bytes)
        {
            ImageSource imgSource = null;
            try
            {
                imgSource = ImageSourceCreation.CreateImageSourceFromBitmap(bitmap, bytes);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return imgSource;
        }

        public static Bitmap ReadBitmapFromByteStream(this byte[] bytes)
        {
            return Image.FromStream(new MemoryStream(bytes), true, false) as Bitmap;
        }

        public static Bitmap CreateThumbnail(this Bitmap img, int oldWidth, int oldHeight, int thumbWidth, int thumbHeight, RenderSettings settings)
        {
            if (oldWidth == thumbWidth && oldHeight == thumbHeight)
                // Untested path
                return img.Clone() as Bitmap;
            else
                return img.CreateBitmap(oldWidth, oldHeight, thumbWidth, thumbHeight, true, settings);
        }        
    }
}
